﻿using System;
using System.Drawing;
using System.Resources;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace raktajino
{
    public partial class UserInputDialog : Form
    {
        public UserInputDialog()
        {
            InitializeComponent();
        }
        
        public Package installingPackage = null;
        int Step = 0;            
        UserInputParameter currentParameter = new UserInputParameter();
        ResourceManager LocRM = null;

        private void UserInputDialog_Load(object sender, EventArgs e)
        {
            LocRM = new ResourceManager("raktajino.WinformStrings", typeof(RAKTAJINO).Assembly);
            this.Text = LocRM.GetString("UserInputDialog_Dialogtitle");
        
            StartWizard();   
        }
        
        private void StartWizard()
        {
        
            btnNextFinish.Enabled = true;
            
            AddControl(this.installingPackage.UserInput.Parameters[Step]);

            if (Step < this.installingPackage.UserInput.Parameters.Count - 1)
            {
                btnNextFinish.Text = LocRM.GetString("Next");
            }
            else
            {
                btnNextFinish.Text = LocRM.GetString("Start");
            }
            btnNextFinish.Select();

        }

        void btnNextFinish_Click(object sender, EventArgs e)
        {
            string textResult = string.Empty;

            switch (this.currentParameter.InputType)
            {
                case UserInputType.uiTypeBool:
                    this.currentParameter.Result = ((CheckBox)this.border.Controls["LogicBox"]).Checked;
                    break;
                case UserInputType.uiTypeString:
                    textResult = ((TextBox)this.border.Controls["RegExTextbox"]).Text;
                    if (this.currentParameter.RegularExpression != string.Empty && this.currentParameter.RegularExpression != null)
                    {
                        try
                        {
                            if (!Regex.IsMatch(textResult, this.currentParameter.RegularExpression))
                            {
                                string msg = string.Format(LocRM.GetString("value_does_not_match_template"));
                                DialogResult dr = MessageBox.Show(this, msg, LocRM.GetString("Invalid_Input"),
                                                                                        MessageBoxButtons.RetryCancel,
                                                                                        MessageBoxIcon.Error);
                                if (dr == DialogResult.Cancel) this.DialogResult = dr;
                                if (dr == DialogResult.Retry)
                                {
                                    StartWizard();
                                    return;
                                }
                            }
                        }
                        catch (ArgumentException)
                        {
                            string msg = string.Format("{0}: {1}", LocRM.GetString("Invalid_Regular_Expression"), this.currentParameter.RegularExpression);
                            MessageBox.Show(this, msg, LocRM.GetString("Invalid_Expression"), MessageBoxButtons.OK, MessageBoxIcon.Error);
                            this.DialogResult = DialogResult.Cancel;
                            return;
                        }
                    }
                    this.currentParameter.Result = textResult;
                    break;

                case UserInputType.uiTypePWDString:
                    textResult = ((TextBox)this.border.Controls["RegExPWDTextbox"]).Text;
                    if (this.currentParameter.RegularExpression != string.Empty && this.currentParameter.RegularExpression != null)
                    {
                        try
                        {
                            if (!Regex.IsMatch(textResult, this.currentParameter.RegularExpression))
                            {
                                string msg = string.Format("The value you entered does not match the template!");
                                DialogResult dr = MessageBox.Show(this, msg, "Invalid Input",
                                                                                        MessageBoxButtons.RetryCancel,
                                                                                        MessageBoxIcon.Error);
                                if (dr == DialogResult.Cancel) this.DialogResult = dr;
                                if (dr == DialogResult.Retry)
                                {
                                    StartWizard();
                                    return;
                                }
                            }
                        }
                        catch (ArgumentException)
                        {
                            string msg = string.Format("Invalid Regular Expression: {0}", this.currentParameter.RegularExpression);
                            MessageBox.Show(this, msg, "Invalid Expression", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            this.DialogResult = DialogResult.Cancel;
                            return;
                        }
                    }
                    this.currentParameter.Result = textResult;
                    break;

                default:
                    break;
            }

            this.installingPackage.UserInput.Parameters[Step] = this.currentParameter;
            Step++;
            if (Step < this.installingPackage.UserInput.Parameters.Count)
            {
                StartWizard();
            }
            else
            {
                this.DialogResult = DialogResult.OK;
            }
        }
        
        
        private void AddControl(UserInputParameter InputParameter)
        {
            this.currentParameter = InputParameter;
            int ControlCount = this.border.Controls.Count;

            for (int i = 0; i < ControlCount; i++)
            {
                this.border.Controls.RemoveAt(0);
            }
        
            this.border.Refresh();
            this.Refresh();
            
            Padding padding = new Padding(5);

            Font Font = new Font("Microsoft Sans Serif", 9);
            
            Label Description = new Label();
            Description.Font = Font;
            Description.AutoSize = true;
            Description.Text = InputParameter.Description;
            border.Controls.Add(Description);

            CheckBox checkbox;
            TextBox textbox;

            switch (InputParameter.InputType)
            {
                case UserInputType.uiTypeBool:
                    checkbox = new CheckBox();
                    Description.Text = "Please decide this logical option!";
                    checkbox.Name = "LogicBox";
                    checkbox.Top = Description.Bottom + 15;
                    checkbox.Text = InputParameter.Description;
                    checkbox.AutoSize = true;
                    checkbox.Font=Font;
                    if (InputParameter.Default.ToLower() == true.ToString().ToLower()) checkbox.Checked = true;
                    border.Controls.Add(checkbox);
                    break;
                case UserInputType.uiTypeString:
                    textbox = new TextBox();
                    textbox.Name = "RegExTextbox";
                    textbox.Anchor = (AnchorStyles.Left | AnchorStyles.Right);
                    textbox.Top = Description.Bottom + 15;
                    textbox.Width = border.Width - 10;
                    textbox.Font = Font;
                    textbox.Text = InputParameter.Default;
                    border.Controls.Add(textbox);
                    break;
                case UserInputType.uiTypePWDString:
                    textbox = new TextBox();
                    textbox.Name = "RegExPWDTextbox";
                    textbox.UseSystemPasswordChar = true;
                    textbox.Anchor = (AnchorStyles.Left | AnchorStyles.Right);
                    textbox.Top = Description.Bottom + 15;
                    textbox.Width = border.Width - 10;
                    textbox.Font = Font;
                    textbox.Text = InputParameter.Default;
                    border.Controls.Add(textbox);
                    break;
                default:
                    break;
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }


    }
}
